package com.zch.starter.metrics.prometheus.springboot.starter.instance.heart;


import com.zch.starter.metrics.prometheus.springboot.starter.config.properties.HeartBeatConfig;
import com.zch.starter.metrics.prometheus.springboot.starter.instance.actuator.LivenessHealthIndicator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 应用实例心跳
 * 如果应用假死了 则不会定时执行了，那个最新的健康心跳时间就会停滞不动了
 */
@Slf4j
public class InstanceHeart {
    @Resource
    private HeartBeatConfig heartBeatConfig;
    @Resource
    private LivenessHealthIndicator livenessHealthIndicator;

    @PostConstruct
    public void init(){
        System.out.println(heartBeatConfig.getInterval());
    }
    // 每5秒执行一次心跳
    @Scheduled(fixedRateString = "${app.heartbeat.interval:5000}")
    public void applicationInstanceHeart() {
        try {
            log.info("心跳执行");
            // 记录健康心跳时间
           livenessHealthIndicator.health();

        } catch (Throwable e) {
            // todo 添加告警
            e.printStackTrace();
        }
    }


}
